//
//  RHEndpointListCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/5/16.
//

#import "RHEndpointListCell.h"
@interface RHEndpointListCell()
@property (nonatomic ,strong) UIImageView *markV;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUILabel *softVerLab;//软件版本

@property (nonatomic ,strong) QMUILabel *hardVerLab;//硬件版本

@property (nonatomic ,strong) QMUILabel *protocolLab;//内网协议

@property (nonatomic ,strong) QMUILabel *belongDeviceLab;//所属设备

@end
@implementation RHEndpointListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setEndpointModel:(RHEndPointModel *)endpointModel
{
    
    _endpointModel = endpointModel;
    
    self.softVerLab.text = ValidNullDataToString(endpointModel.lastVersion, @"--");
    self.hardVerLab.text = ValidNullDataToString(endpointModel.hardVersion, @"--");
    self.protocolLab.text = ValidNullDataToString(endpointModel.prtclVer, @"--");
    self.belongDeviceLab.text = ValidNullDataToString(endpointModel.deviceName, @"--");
    self.titleLab.text = self.endpointModel.sn;
    self.markV.backgroundColor = CMainColorGreen;
    
    //状态显示优先权  离线->故障->告警->保护->正常
    if(endpointModel.state == 1){
        self.markV.backgroundColor = kCOLOR_HEX(@"#28B911");//绿
    }
    
    //1:告警 2:故障 3:保护
    if (endpointModel.workState > 0) {//告警
        self.markV.backgroundColor = kCOLOR_HEX(@"#F95454");//红
    }
    
    if (endpointModel.state == 0) {
        self.markV.backgroundColor = kCOLOR_HEX(@"#A8ABB2");//灰
    }
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:CFTabViewBgColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(8);
        make.bottom.mas_offset(-8);
    }];
    
    _markV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
    [_markV.layer setCornerRadius:4];
    [bgView addSubview:_markV];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:[UIFont systemFontOfSize:14]];
    [_titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    _titleLab.adjustsFontSizeToFitWidth = YES;
    _titleLab.minimumScaleFactor = 0.8;
    [bgView addSubview:_titleLab];
    
   
    
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];

  
    //-------------------------
    
    QMUILabel *softVerTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    softVerTitleLab.text = [NSString stringWithFormat:@"%@:",@"软件版本".rh_localized];
    [bgView addSubview:softVerTitleLab];
    [softVerTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(lineV.mas_bottom).offset(15);
    }];
    
    _softVerLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _softVerLab.text = @"--";
    [bgView addSubview:_softVerLab];
    [_softVerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(softVerTitleLab);
    }];
    
   
    QMUILabel *hardVerTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    hardVerTitleLab.text = [NSString stringWithFormat:@"%@:",@"硬件版本".rh_localized];
    [bgView addSubview:hardVerTitleLab];
    [hardVerTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(softVerTitleLab.mas_bottom).offset(15);
    }];
    
    _hardVerLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _hardVerLab.text = @"--";
    [bgView addSubview:_hardVerLab];
    [_hardVerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(hardVerTitleLab);
    }];
   
    
    
    
    QMUILabel *protocolTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    protocolTitleLab.text = [NSString stringWithFormat:@"%@:",@"内网协议".rh_localized];
    [bgView addSubview:protocolTitleLab];
    [protocolTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(hardVerTitleLab.mas_bottom).offset(15);
    }];
    
    _protocolLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _protocolLab.text = @"--";
    [bgView addSubview:_protocolLab];
    [_protocolLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(protocolTitleLab);
    }];
    
    QMUILabel *firmwareTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    firmwareTitleLab.text = [NSString stringWithFormat:@"%@:",@"所属设备".rh_localized];
    [bgView addSubview:firmwareTitleLab];
    [firmwareTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(protocolTitleLab.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
    
    _belongDeviceLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _belongDeviceLab.text = @"--";
    [bgView addSubview:_belongDeviceLab];
    [_belongDeviceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(firmwareTitleLab);

    }];
    
    
    
    
    [_markV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(8, 8));
        make.left.mas_offset(15);
        make.top.mas_offset(16);
    }];
    
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.left.equalTo(weakSelf.markV.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.markV);
    }];
    
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.left.equalTo(weakSelf.markV);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.markV.mas_bottom).offset(16);
    }];
}
@end
